/*
 * @Author: wh
 * @Description:  nodejs定时爬取百度实时热搜
 * @Date: 2020-05-08 
 * @Last Modified by: 
 * @Last Modified time: 
 * 程序员导航站：https://iiter.cn
 */
const cheerio = require("cheerio");
const superagent = require("superagent");
require('superagent-charset')(superagent);
const fs = require("fs");
const nodeSchedule = require("node-schedule");
const weiboURL = "http://top.baidu.com";
const hotSearchURL = weiboURL + "/?fr=mhd_card";

/**
 * 获取热搜列表数据方法
 * 
 * dependencies 生产 vue react
 *  npm i packageName (-save || blank)
 * 
 * devDependencies 开发 webpack glup
 *  npm i packageName -save-dev
 *  
 */
function getHotSearchList() {
  return new Promise((resolve, reject) => {
    // gbk2312 - 百度
    superagent
      .get(hotSearchURL)
      .charset('gb2312')
      .end((err, res) => {
        if (err) reject("request error");
        // console.log(res.text)
  
        const $ = cheerio.load(res.text, {decodeEntities: false});
        let hotList = [];
        $("#main #hot-list li").each(function (index) {
          if (index) {
            // console.log($(this))
            const $li = $(this).find('a')
            const link = $li.attr("href")
            // const text = $li.attr("title")
            const text = $li.text()
            // console.log(text)
            // const $td = $(this).children().eq(1);
            // const link = weiboURL + $td.find("a").attr("href");
            // const text = $td.find("a").text();
            // const hotValue = $td.find("span").text();
            // const icon = $td.find("img").attr("src")
            //   ? "https:" + $td.find("img").attr("src")
            //   : "";
            // console.log(text)
            hotList.push({
              index,
              link,
              text,
              // hotValue,
              // icon,
            });
          }
        });
        hotList.length ? resolve(hotList) : reject("errer");
    });
  });
}

/*
 * schedule

*    *    *    *    *    *    
┬    ┬    ┬    ┬    ┬    ┬
│    │    │    │    │    │
│    │    │    │    │    └ day of week (0 - 7) (0 or 7 is Sun)
│    │    │    │    └───── month (1 - 12)
│    │    │    └────────── day of month (1 - 31)
│    │    └─────────────── hour (0 - 23)
│    └──────────────────── minute (0 - 59)
└───────────────────────── second (0 - 59, OPTIONAL)

 */
/**
 * 每分钟第30秒定时执行爬取任务
 */
nodeSchedule.scheduleJob("30 * * * * *", async function () {
  try {
    const hotList = await getHotSearchList();
    // console.log(hotList[0])
    await fs.writeFileSync(
      `${__dirname}/hotSearchBd.json`,
      JSON.stringify(hotList),
      "utf-8"
    );
    console.log("写入成功", Date.now());
  } catch (error) {
    console.error(error);
  }
});
